﻿/*
=========================================================
功能：      将编辑页面转换成查看页面
修改人：    于维海
修改时间：  2009-7-7
=========================================================
*/
var isIE = false; //判断是否为IE浏览器
if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) {
    isIE = true;
}

//为火狐增浏览器加insertAdjacentElement方法
if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
    if (typeof HTMLElement != "undefined" && !HTMLElement.prototype.insertAdjacentElement) {
        HTMLElement.prototype.insertAdjacentElement = function (where, parsedNode) {
            switch (where) {
                case 'beforeBegin':
                    this.parentNode.insertBefore(parsedNode, this)
                    break;
                case 'afterBegin':
                    this.insertBefore(parsedNode, this.firstChild);
                    break;
                case 'beforeEnd':
                    this.appendChild(parsedNode);
                    break;
                case 'afterEnd':
                    if (this.nextSibling) this.parentNode.insertBefore(parsedNode, this.nextSibling);
                    else this.parentNode.appendChild(parsedNode);
                    break;
            }
        }

        HTMLElement.prototype.insertAdjacentHTML = function (where, htmlStr) {
            var r = this.ownerDocument.createRange();
            r.setStartBefore(this);
            var parsedHTML = r.createContextualFragment(htmlStr);
            this.insertAdjacentElement(where, parsedHTML)
        }

        HTMLElement.prototype.insertAdjacentText = function (where, txtStr) {
            var parsedText = document.createTextNode(txtStr)
            this.insertAdjacentElement(where, parsedText)
        }
    }
}

function ChangeToLabel() {
    //fileUpload div
    var inputs = null;
    inputs = isIE ? document.all.tags("div") : document.getElementsByTagName("div");
    for (var j = 0; j < inputs.length; j++) {
        var ctrl = inputs[j];
        if (ctrl.name == "UploadFileDiv") {
            ctrl.style.display = "none";
            ctrl.style.visibility = "hidden";
        }
        //显示或隐藏内容（ishow）
        if (ctrl.attributes["ishow"] != null) {
            if (ctrl.attributes["ishow"].value.toLowerCase() == "true") {
                ctrl.style.display = "";
                ctrl.style.visibility = "visible";
            }
            else {
                ctrl.style.display = "none";
                ctrl.style.visibility = "hidden";
            }
        }
    }
    //File upload detele link
    inputs = null;
    inputs = isIE ? document.all.tags("a") : document.getElementsByTagName("a");
    for (var j = 0; j < inputs.length; j++) {
        var ctrl = inputs[j];
        if (ctrl != null && ctrl.name == "DeleteFileLink") {
            ctrl.style.display = "none";
        }
        //显示或隐藏内容（ishow）
        if (ctrl.attributes["ishow"] != null) {
            if (ctrl.attributes["ishow"].value.toLowerCase() == "true") {
                ctrl.style.display = "";
                ctrl.style.visibility = "visible";
            }
            else {
                ctrl.style.display = "none";
                ctrl.style.visibility = "hidden";
            }
        }
    }

    //span
    inputs = null;
    inputs = isIE ? document.all.tags("span") : document.getElementsByTagName("span");
    for (var j = 0; j < inputs.length; j++) {
        var ctrl = inputs[j];
        if (ctrl != null && ctrl.name == "ValueData") {
            ctrl.style.display = "none";
        }

        //显示或隐藏内容（ishow）
        if (ctrl.attributes["ishow"] != null) {
            if (ctrl.attributes["ishow"].value.toLowerCase() == "true") {
                ctrl.style.display = "";
                ctrl.style.visibility = "visible";
            }
            else {
                ctrl.style.display = "none";
                ctrl.style.visibility = "hidden";
            }
        }
        if (ctrl.attributes["ischange"] != null && ctrl.attributes["ischange"].value.toLowerCase() == "true")
            ctrl.outerHTML = "<div style=\"width:" + ctrl.offsetWidth + ";border-bottom:1 solid #FF0000\">" + ctrl.outerHTML + "</div>";
    }

    //大文本框
    inputs = null;
    inputs = isIE ? document.all.tags("textarea") : document.getElementsByTagName("textarea");
    if (inputs != null) {
        for (var j = 0; j < inputs.length; j++) {
            var ctrl = inputs[j];
            if (ctrl != null) {
                ctrl.insertAdjacentText("afterEnd", ctrl.value);
                ctrl.style.display = "none";
            }

        }
    }

    //图片
    inputs = null;
    inputs = isIE ? document.all.tags("img") : document.getElementsByTagName("img");
    if (inputs != null) {
        for (var j = 0; j < inputs.length; j++) {
            var ctrl = inputs[j];
            if (ctrl != null && ctrl.attributes["ishow"] == "false") {
                ctrl.style.display = "none";
            }
        }
    }
    //下拉框
    inputs = null;
    inputs = isIE ? document.all.tags("select") : document.getElementsByTagName("select");
    if (inputs != null) {
        for (var j = 0; j < inputs.length; j++) {
            var ctrl = inputs[j];
            //如果该控件要求被隐藏 则直接返回到循环头部
            if (ctrl.attributes["ishow"] != null && ctrl.attributes["ishow"] == "false") {
                ctrl.style.display = "none";
                continue;
            }
            if (ctrl != null && ctrl.options.length > 0) {
                if (navigator.userAgent.indexOf("Firefox") == -1 && ctrl.attributes["ischange"] != null && ctrl.attributes["ischange"].value.toLowerCase() == "false") {
                    //在控件上放置一个透明的div，使控件不能被点击
                    ctrl.insertAdjacentHTML("beforeBegin", "<div style=\"background-color:#ffffff;position:absolute;height:" + ctrl.offsetHeight + ";width:" + ctrl.offsetWidth + ";filter:alpha(opacity = 0)\"></div>");
                    ctrl.onfocus = function () { this.blur(); }
                }
                else {
                    if (ctrl.selectedIndex != -1) {
                        ctrl.insertAdjacentHTML("afterEnd", "<font class=LableLine>" + ctrl.options[ctrl.selectedIndex].text + "</font>");
                    }
                    ctrl.style.display = "none";
                }
            }
            else if (ctrl != null) {
                ctrl.insertAdjacentHTML("afterEnd", "<font class=LableLine></font>");
                ctrl.style.display = "none";
            }
        }
    }

    inputs = null;
    inputs = isIE ? document.all.tags("input") : document.getElementsByTagName("input");
    for (var j = 0; j < inputs.length; j++) {
        var ctrl = inputs[j];
        if (ctrl != null) {
            if (ctrl.attributes["ishow"] != null) {
                if (ctrl.attributes["ishow"].value.toLowerCase() == "true") {
                    ctrl.style.display = "";
                    ctrl.style.visibility = "visible";
                }
                else {
                    ctrl.style.display = "none";
                    ctrl.style.visibility = "hidden";
                }
                continue;
            }
            var ctrlType = inputs[j].type.toLowerCase();
            switch (ctrlType) {
                case "checkbox":
                    //在控件上放置一个透明的div，使控件不能被点击
                    if (isIE)
                        ctrl.insertAdjacentHTML("beforeBegin", "<div style=\"background-color:#ffffff;position:absolute;height:20px;width:20px;filter:alpha(opacity = 0)\"></div>");
                    else
                        ctrl.insertAdjacentHTML("beforeBegin", "<div style=\"background-color:#ffffff;position:absolute;height:" + ctrl.parentNode.offsetHeight + "px;width:" + ctrl.parentNode.offsetWidth + "px;opacity:0;-moz-opacity:0;\"></div>");
                    ctrl.onfocus = function () { this.blur(); }
                    break;
                case "radio":
                    if (ctrl.attributes["ischange"] != null && ctrl.attributes["ischange"].value.toLowerCase() == "false") {
                        var ra = document.getElementsByName(ctrl.name);
                        var lab = document.getElementsByTagName("label"); ;

                        for (var i = 0; i < ra.length; i++) {
                            ra[i].style.display = "none";
                            if (!ra[i].checked) {
                                if (lab != null) {
                                    for (var j = 0; j < lab.length; j++) {
                                        if (lab[j].htmlFor == ra[i].id)
                                            lab[j].style.display = "none";
                                    }
                                }
                            }
                        }
                    }
                    else {
                        //在控件上放置一个透明的div，使控件不能被点击
                        if (isIE)
                            ctrl.insertAdjacentHTML("beforeBegin", "<div style=\"background-color:#ffffff;position:absolute;height:20px;width:20px;filter:alpha(opacity = 0)\"></div>");
                        else
                            ctrl.insertAdjacentHTML("beforeBegin", "<div style=\"background-color:#ffffff;position:absolute;height:" + ctrl.parentNode.offsetHeight + "px;width:" + ctrl.parentNode.offsetWidth + "px;opacity:0;-moz-opacity:0;\"></div>");
                        ctrl.onfocus = function () { this.blur(); }
                    }
                    break;
                case "text":
                    if (ctrl.DataField != "Appid" || ctrl.DataField != "登录名") {
                        if (ctrl.style.display != "none") {
                            if (ctrl.attributes["ischange"] != null && ctrl.attributes["ischange"].value.toLowerCase() == "true") {
                                var control = ctrl.value.replace("\r\n", "<br>") == "" ? "" : ctrl.value.replace("\r\n", "<br>");
                                ctrl.insertAdjacentHTML("afterEnd", "<span style='border-bottom:1 solid #FF0000'><font class=LableLine>" + control + "</font></span>");
                            }
                            else
                                ctrl.insertAdjacentHTML("afterEnd", "<font class=LableLine>" + ctrl.value.replace("\r\n", "<br>") + "</font>");
                        }
                        ctrl.style.display = "none";
                        //日期控件
                        if ($(ctrl).parent().attr("name") == "datetimepicker") {
                            $(ctrl).parent().attr("class", "");
                            $(ctrl).parent().children(".add-on").hide();
                        }
                    }
                    break;
                case "select-one":
                    if (ctrl.options.length > 0) {
                        if (ctrl.attributes["ischange"] != null && ctrl.attributes["ischange"].value.toLowerCase() == "true")
                            ctrl.insertAdjacentHTML("afterEnd", "<span style='border-bottom:1 solid #FF0000'><font class=LableLine>" + ctrl.options[ctrl.selectedIndex].text + "</font></span>");
                        else
                            ctrl.insertAdjacentHTML("afterEnd", "<font class=LableLine>" + ctrl.options[ctrl.selectedIndex].text + "</font>");
                        ctrl.style.display = "none";
                    }
                    else {
                        ctrl.insertAdjacentHTML("afterEnd", "<font class=LableLine></font>");
                        ctrl.style.display = "none";
                    }
                    break;
                case "button":
                    if (ctrl.attributes["ishow"] != "true" && ctrl.name != "returnInput")
                        ctrl.style.display = "none";
                    break;
                case "submit":
                    if (ctrl.attributes["ishow"] != "true")
                        ctrl.style.display = "none";
                    break;
                case "textarea":
                    if (ctrl.attributes["ischange"] != null && ctrl.attributes["ischange"].value.toLowerCase() == "true") {
                        var control = ctrl.value.replace("\r\n", "<br>").replace(" ", "") == "" ? "" : ctrl.value.replace("\r\n", "<br>").replace(" ", "");
                        ctrl.insertAdjacentHTML("afterEnd", "<span style='border-bottom:1 solid #FF0000'>" + control + "</span>");
                    }
                    else
                        ctrl.insertAdjacentHTML("afterEnd", ctrl.value.replace("\r\n", "<br>").replace(" ", "") + "");
                    ctrl.style.display = "none";
                    //						ctrl.className = "flowcontentShow";
                    //						ctrl.readOnly = true;
                    //						ctrl.title = "";

                    break;

                case "img":
                    ctrl.style.display = "none";
                    break;
                default:
                    break;
            }
        } // end if
    }

    //checkbox的label 去除for属性
    inputs = null;
    inputs = isIE ? document.all.tags("label") : document.getElementsByTagName("label"); ;
    if (inputs != null) {
        for (var j = 0; j < inputs.length; j++) {
            var ctrl = inputs[j];
            ctrl.htmlFor = "";
        }
    }
}

function SetOneCtrlToLabel(ctrlname) {

    var ctrl = document.getElementById(ctrlname);
    var ctrlType;
    var tempValue = '';

    try { ctrlType = ctrl.type.toLowerCase(); }
    catch (e) {
        ctrlType = '';
    }
    try {
        if (ctrl.style.display == "none")
            return;
    }
    catch (e) { }

    switch (ctrlType) {
        case "radio":
            if (ctrl.checked) {
                ctrl.insertAdjacentHTML("afterEnd", "<font class=LableLine>是</font>");
            }
            ctrl.style.display = "none";
            break;

        case "text":
            ctrl.insertAdjacentHTML("afterEnd", "<font class=LableLine>" + ctrl.value + "</font>");
            ctrl.style.display = "none";
            break;

        case "select-one":
            if (ctrl.options.length > 0) {
                ctrl.insertAdjacentHTML("afterEnd", "<font class=LableLine>" + ctrl.options[ctrl.selectedIndex].text + "</font>");
                ctrl.style.display = "none";
            }
            else {
                ctrl.insertAdjacentHTML("afterEnd", "<font class=LableLine></font>");
                ctrl.style.display = "none";
            }
            break;

        case "textarea":
            ctrl.insertAdjacentHTML("afterEnd", ctrl.value.replace("\r\n", "<br>").replace(" ", "") + "");
            ctrl.style.display = "none";
            break;

        case "undefined":
            ctrl.style.display = "none";
            break;

        default:
            break;
    }
}